package yxy.game.pm2.command.pickedtroops;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.commandertally;
import yxy.cherry.data.bean.commandertallyrandom;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PickedTroopsSlot;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedTroopsMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.pickedtroops.PickedTroopsHandle;

import java.util.ArrayList;


/****************************************
 * @name CMD_PickedTroopsSlotRecoinInfo
 * @description 兵符重铸信息
 * @author 梁健彬
 * @since 2022-05-20
 ***************************************/
@MessageType(proto = Command.PickedTroopsSlotRecoinInfo, classtype = PickedTroopsMessage.PickedTroopsSlotRecoinInfoRequest.class)
public class CMD_PickedTroopsSlotRecoinInfo extends Command<PickedTroopsMessage.PickedTroopsSlotRecoinInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedTroopsMessage.PickedTroopsSlotRecoinInfoRequest request) {
        String playerCode = messageSource.getTag();//playercode
        String slotCode = request.getSlotCode();//兵符code
        String idArrary = request.getIdArrary();//需要保护的词条集合


        PickedTroopsMessage.PickedTroopsSlotRecoinInfoResponse.Builder response = PickedTroopsMessage.PickedTroopsSlotRecoinInfoResponse.newBuilder();

        try {

            JSONArray jsonArray = JSONArray.parseArray(idArrary);//需要保护的词条集合

            //判断兵符是否存在
            PickedTroopsSlot slot = PickedTroopsSlot.get(playerCode, slotCode);
            if (slot == null) {
                send(messageSource, response, Result.ParamError);
                return;
            }

            //兵符的表
            commandertally data = DataCenter.getData(slot.getDataId(), commandertally.class);

            //不能全部词条都保护
            if (jsonArray != null && data.getEntrynum() == jsonArray.size()) {
                send(messageSource, response, Result.PickedTroopsSlotEntrynumMax);
                return;
            }

            ArrayList<Currency> rewards = new ArrayList<>();
            //词条保护的消耗
            for (int i = 0; jsonArray != null && i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                commandertallyrandom commandertallyrandom = DataCenter.getData(jsonObject.getIntValue("id"), yxy.cherry.data.bean.commandertallyrandom.class);
                rewards.add(Currency.create(commandertallyrandom.getTrait(), commandertallyrandom.getAttack(), commandertallyrandom.getDefense()));
            }

            //兵符重铸的消耗
            rewards.add(Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum()));

            try {
                new CurrencyHandle().cost(playerCode, CCReason.兵符, rewards);
            } catch (CurrencyNotEnoughException e) {
                send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
                return;
            }

            //重铸
            slot = PickedTroopsSlot.recoin(playerCode, slot, jsonArray, data);

            //重铸并返回重铸后的词条信息
            response.setSlot(PickedTroopsHandle.getSlot(slot));

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }

    }

    private void send(MessageSource messageSource, PickedTroopsMessage.PickedTroopsSlotRecoinInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}